<?php
// +--------------------------------------------------------------------------+
// | Image_Graph aka GraPHPite                                                |
// +--------------------------------------------------------------------------+
// | Copyright (C) 2003, 2004 Jesper Veggerby Hansen                          |
// | Email         pear.nosey@veggerby.dk                                |
// | Web           http://graphpite.sourceforge.net                           |
// | PEAR          http://pear.php.net/pepr/pepr-proposal-show.php?id=145     |
// +--------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or            |
// | modify it under the terms of the GNU Lesser General Public               |
// | License as published by the Free Software Foundation; either             |
// | version 2.1 of the License, or (at your option) any later version.       |
// |                                                                          |
// | This library is distributed in the hope that it will be useful,          |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        |
// | Lesser General Public License for more details.                          |
// |                                                                          |
// | You should have received a copy of the GNU Lesser General Public         |
// | License along with this library; if not, write to the Free Software      |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA |
// +--------------------------------------------------------------------------+

/**
 * Image_Graph aka GraPHPite - PEAR PHP OO Graph Rendering Utility.
 * @package dataset
 * @copyright Copyright (C) 2003, 2004 Jesper Veggerby Hansen
 * @license http://www.gnu.org/licenses/lgpl.txt GNU Lesser General Public License
 * @author Jesper Veggerby Hansen <pear.nosey@veggerby.dk>
 * @version $Id: Function.php,v 1.5 2004/11/05 19:13:30 nosey Exp $
 */ 

/**
 * Include file Graph/Dataset.php
 */
require_once(IMAGE_GRAPH_PATH . "/Graph/Dataset.php");

/**
 * Function data set, points are generated by calling an external function.
 * The function must be a single variable function and return a the result, builtin functions
 * that are fx sin() or cos().
 * User defined function can be used if they are such, i.e: function myFunction($variable)
 * User defined function can be used if they are such, i.e: {
 */
class Image_Graph_Dataset_Function extends Image_Graph_Dataset 
{

    /**
     * The name of the function
     * @var string
     * @access private
     */
    var $_dataFunction;

    /**
     * Image_Graph_FunctionDataset [Constructor]
     * @param double $minimumX The minimum X value
     * @param double $maximumX The maximum X value
     * @param string $function The name of the function, if must be a single parameter function like fx sin(x) or cos(x)
     * @param int Points The number of points to create
     */
    function &Image_Graph_Dataset_Function($minimumX, $maximumX, $function, $points)
    {
        parent::Image_Graph_Dataset();
        $this->_minimumX = $minimumX;
        $this->_maximumX = $maximumX;
        $this->_dataFunction = $function;
        $this->_count = $points;
        $this->_calculateMaxima();
    }

    /**
     * Add a point to the dataset.
     * You can't add points to a function dataset
     * @param int $x The X value to add
     * @param int $y The Y value to add, can be omited
     * @param var $ID The ID of the point
	 */
    function addPoint($x, $y = false, $ID = false)
    {
    }

    /**
     * Gets a Y point from the dataset
     * @param var $x The variable to apply the function to
     * @return var The function applied to the X value
     * @access private
  	 */
    function _getPointY($x)
    {
        $function = $this->_dataFunction;
        return $function ($x);
    }

    /**
     * The number of values in the dataset
     * @return int The number of values in the dataset
     * @access private
	 */
    function _count()
    {
        return $this->_count;
    }

    /**
     * The interval between 2 adjacent Y values
     * @return var The interval
     * @access private
	 */
    function _stepX()
    {
        return ($this->_maximumX - $this->_minimumX) / $this->_count();
    }

    /**
     * Calculates the Y extrema of the function
     * @access private
	 */
    function _calculateMaxima()
    {
        $x = $this->_minimumX;
        while ($x <= $this->_maximumX) {
            $y = $this->_getPointY($x);
            $this->_minimumY = min($y, $this->_minimumY);
            $this->_maximumY = max($y, $this->_maximumY);
            $x += $this->_stepX();
        }
    }

}

?>